
import {Injectable} from "@angular/core";

@Injectable()
export class FormUtil {

  serialize(o:Object):string{
    return this.serializeForm(o);
  }


  /**
   * 根据code获取相应的数据字典
   * @param val
   */
  filterDictionaryByCode(code:any,dicList:any):Array<any>{
    return  dicList.filter(function(value :any, index:any, array:any){
      if(value.code != code && value.code.indexOf(code) != -1){
        return value ;
      }
    });
  }
  private serializeForm(o:Object){
    let keys = Object.getOwnPropertyNames(o);
    let result: string = "";

    keys.forEach((key, index, array) => {
      let value = o[key];
      //判断值是否为null or undefine
      if(value != null && value != 'null' && value != "undefine"){
        result += key + "=" + value + "&";
      }
    });

    //去除最后的“&”连接符
    if(result.length > 0 && result.endsWith("&")){
      result = result.substring(0,result.length - 1 );
    }
    return result;
  }


}
